package grape.composite.rest.feedback.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.AbstractLoginUser;
import grape.common.exception.runtime.InvalidParamsException;
import grape.common.service.loginuser.LoginUser;
import grape.composite.rest.feedback.form.FeedbackCreateForm;
import grape.composite.rest.feedback.form.FeedbackReplyForm;
import grape.composite.rest.feedback.mapper.FeedbackWebMapper;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;

import grape.composite.rest.feedback.form.FeedbackListPageForm;
import grape.composite.rest.feedback.vo.FeedbackVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.composite.service.feedback.po.Feedback;
import grape.composite.service.feedback.api.IFeedbackService;

/**
 * <p>
 * 意见反馈表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-03-15
 */
@RestController
@RequestMapping("/composite/feedback")
@Api(tags = "意见反馈相关接口")
public class FeedbackController extends BaseController<FeedbackVo, Feedback> {

    @Autowired
    private FeedbackWebMapper currentWebMapper;
    @Autowired
    private IFeedbackService currentService;



     @ApiOperation("添加意见反馈")
     @PreAuthorize("hasAuthority('user')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public FeedbackVo create(@RequestBody @Valid FeedbackCreateForm cf) {
         Feedback po = currentWebMapper.formToPo(cf);
         LoginUser loginUser = AbstractLoginUser.getLoginUser();
         if (loginUser != null) {
             po.setUserId(loginUser.getId());
         }
         po.setSuggestionAt(System.currentTimeMillis());
         return super.create(po);
     }

     @ApiOperation("根据ID查询意见反馈")
     @PreAuthorize("hasAuthority('user')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public FeedbackVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除意见反馈")
     @PreAuthorize("hasAuthority('feedback:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         if (currentService.countByPreviousFeedbackId(id) > 0) {
             throw new InvalidParamsException("存在关联的意见反馈，不能删除");
         }
         return super.deleteById(id);
     }

     @ApiOperation("回复意见反馈")
     @PreAuthorize("hasAuthority('feedback:single:replyById')")
     @PutMapping("/reply/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public FeedbackVo update(@PathVariable String id,@RequestBody @Valid FeedbackReplyForm uf) {
         Feedback po = currentWebMapper.formToPo(uf);
         po.setId(id);
         po.setReplyAt(System.currentTimeMillis());
         return super.update(po);
     }

    @ApiOperation("分页查询意见反馈")
    @PreAuthorize("hasAuthority('feedback:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<FeedbackVo> listPage(FeedbackListPageForm listPageForm) {
         Feedback po = currentWebMapper.formToPo(listPageForm);
         return super.listPage(po,listPageForm);
     }

}
